//-----------------------------------------------------------------------
// <copyright file="a_AddresseDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_AddresseDAOBase
    {
        #region Common methods
        public virtual a_Addresse Createa_AddresseFromReader(IDataReader reader)
        {
            a_Addresse item = new a_Addresse();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("AddrID"))) item.AddrID = (int)reader["AddrID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Ship3rdPartyName"))) item.Ship3rdPartyName = (string)reader["Ship3rdPartyName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) item.Address = (string)reader["Address"];
                if (!reader.IsDBNull(reader.GetOrdinal("Unit"))) item.Unit = (string)reader["Unit"];
                if (!reader.IsDBNull(reader.GetOrdinal("City"))) item.City = (string)reader["City"];
                if (!reader.IsDBNull(reader.GetOrdinal("State"))) item.State = (string)reader["State"];
                if (!reader.IsDBNull(reader.GetOrdinal("Zipcode"))) item.Zipcode = (string)reader["Zipcode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Country"))) item.Country = (string)reader["Country"];
                if (!reader.IsDBNull(reader.GetOrdinal("Type"))) item.Type = (byte)reader["Type"];
                if (!reader.IsDBNull(reader.GetOrdinal("Tel_AreaCode"))) item.Tel_AreaCode = (string)reader["Tel_AreaCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Tel_Num"))) item.Tel_Num = (string)reader["Tel_Num"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDeleted"))) item.IsDeleted = (bool)reader["IsDeleted"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Addresse methods
            
        public virtual void Createa_Addresse(a_Addresse a_Addresse)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Addresses_ins");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Addresse.UserID);
                database.AddInParameter(dbCommand, "@Ship3rdPartyName", DbType.AnsiString, a_Addresse.Ship3rdPartyName);
                database.AddInParameter(dbCommand, "@Address", DbType.AnsiString, a_Addresse.Address);
                database.AddInParameter(dbCommand, "@Unit", DbType.AnsiString, a_Addresse.Unit);
                database.AddInParameter(dbCommand, "@City", DbType.AnsiString, a_Addresse.City);
                database.AddInParameter(dbCommand, "@State", DbType.AnsiString, a_Addresse.State);
                database.AddInParameter(dbCommand, "@Zipcode", DbType.AnsiString, a_Addresse.Zipcode);
                database.AddInParameter(dbCommand, "@Country", DbType.AnsiString, a_Addresse.Country);
                database.AddInParameter(dbCommand, "@Type", DbType.Byte, a_Addresse.Type);
                database.AddInParameter(dbCommand, "@Tel_AreaCode", DbType.AnsiString, a_Addresse.Tel_AreaCode);
                database.AddInParameter(dbCommand, "@Tel_Num", DbType.AnsiString, a_Addresse.Tel_Num);
                database.AddInParameter(dbCommand, "@IsDeleted", DbType.Boolean, a_Addresse.IsDeleted);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Addresse.DomainID);
                database.AddOutParameter(dbCommand, "@AddrID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Addresse.AddrID = (int)database.GetParameterValue(dbCommand, "@AddrID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Addresse methods
        
        public virtual void Updatea_Addresse(a_Addresse a_Addresse)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Addresses_upd");            
                
                database.AddInParameter(dbCommand, "@AddrID", DbType.Int32, a_Addresse.AddrID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Addresse.UserID);
                database.AddInParameter(dbCommand, "@Ship3rdPartyName", DbType.AnsiString, a_Addresse.Ship3rdPartyName);
                database.AddInParameter(dbCommand, "@Address", DbType.AnsiString, a_Addresse.Address);
                database.AddInParameter(dbCommand, "@Unit", DbType.AnsiString, a_Addresse.Unit);
                database.AddInParameter(dbCommand, "@City", DbType.AnsiString, a_Addresse.City);
                database.AddInParameter(dbCommand, "@State", DbType.AnsiString, a_Addresse.State);
                database.AddInParameter(dbCommand, "@Zipcode", DbType.AnsiString, a_Addresse.Zipcode);
                database.AddInParameter(dbCommand, "@Country", DbType.AnsiString, a_Addresse.Country);
                database.AddInParameter(dbCommand, "@Type", DbType.Byte, a_Addresse.Type);
                database.AddInParameter(dbCommand, "@Tel_AreaCode", DbType.AnsiString, a_Addresse.Tel_AreaCode);
                database.AddInParameter(dbCommand, "@Tel_Num", DbType.AnsiString, a_Addresse.Tel_Num);
                database.AddInParameter(dbCommand, "@IsDeleted", DbType.Boolean, a_Addresse.IsDeleted);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Addresse.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Addresse methods
        public virtual void Deletea_Addresse(int addrID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Addresses_del");
                
                database.AddInParameter(dbCommand, "@AddrID", DbType.Int32, addrID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Addresse methods
        
        public virtual a_Addresse Geta_Addresse(int addrID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Addresses_sel");
                
                database.AddInParameter(dbCommand, "@AddrID", DbType.Int32, addrID);
                
                a_Addresse a_Addresse =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Addresse = Createa_AddresseFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Addresse;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_AddresseList methods
        public virtual a_AddresseCollection Geta_AddresseList(a_AddresseColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Addresses_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_AddresseCollection a_AddresseCollection = new a_AddresseCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Addresse a_Addresse = Createa_AddresseFromReader(reader);
                        a_AddresseCollection.Add(a_Addresse);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_AddresseCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_AddresseCollection Geta_AddresseList(a_AddresseColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_AddresseList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}